package com.solution.pc.order.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.shiro.crypto.hash.SimpleHash;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.solution.pc.order.entity.OrderAlipayNotifyLogEntity;
import com.solution.pc.order.service.OrderAlipayNotifyLogService;
import com.solution.common.controller.BaseController;
import com.solution.common.entity.BaseReturn;
import com.solution.common.page.JSONGrid;
import com.solution.common.page.PageBean;
import com.solution.common.page.PageParam;
import com.solution.common.utils.string.StringUtil;

/**
 * 订单支付宝支付日志web控制层
 * @author llp
 * @date 2017-10-19 09:36:39
 */
@RequestMapping("/orderAlipayNotifyLog")
@Controller
public class OrderAlipayNotifyLogController extends BaseController {

	private Logger logger = LoggerFactory.getLogger(OrderAlipayNotifyLogController.class);

	@Autowired
	private OrderAlipayNotifyLogService orderAlipayNotifyLogService;
	
	/**
	 * 跳转订单支付宝支付日志查询页面
	 * @param OrderAlipayNotifyLog 接收界面查询条件参数
	 * @param request 请求对象
	 * @return 返回对应的页面视图
	 * @author llp
 	 * @date 2017-10-19 09:36:39
	 */
	@RequestMapping("/toListOrderAlipayNotifyLogPage")
	public String toListOrderAlipayNotifyLogPage(OrderAlipayNotifyLogEntity orderAlipayNotifyLog, HttpServletRequest request) {
		return "order/orderAlipayNotifyLog_list";
	}

	/**
	 * 分页查询订单支付宝支付日志列表
	 * @param orderAlipayNotifyLog 接收界面查询条件参数
	 * @param request 请求对象
	 * @return 返回对应的页面视图
	 * @author llp
 	 * @date 2017-10-19 09:36:39
	 */
	@RequestMapping("/listOrderAlipayNotifyLogPage")
	@ResponseBody
	public JSONGrid listOrderAlipayNotifyLogPage(OrderAlipayNotifyLogEntity orderAlipayNotifyLog, HttpServletRequest request){
		int pageNum=Integer.parseInt(request.getParameter("page").toString());
		int numPerPage=Integer.parseInt(request.getParameter("rows").toString());
		PageParam pageParam = super.getPageParam(request);
		pageParam.setPageNum(pageNum);
		pageParam.setNumPerPage(numPerPage);
		
		
		
		Map<String, Object> paramMap = new HashMap<String, Object>();
		try {
			PageBean pageBean = orderAlipayNotifyLogService.listOrderAlipayNotifyLogPage(pageParam, paramMap);
			return new JSONGrid(pageBean.getRecordList(),pageBean.getTotalCount());
		} catch (Exception e) {
			handleException("分页查询订单支付宝支付日志失败", logger, e);
		}
		return new JSONGrid();
	}
	
	
	/**
	 * 跳转新增页面
	 * @return string 跳转页面路径
	 * @author llp
 	 * @date 2017-10-19 09:36:39
	 */
	@RequestMapping("/toAddOrderAlipayNotifyLog")
	public String toAddOrderAlipayNotifyLog(HttpServletRequest request) {
		request.setAttribute("opFlag", "add");
		return "order/orderAlipayNotifyLog_form";
	}

	/**
	 * 跳转编辑页面
	 * @return string 跳转页面路径
	 * @author llp
 	 * @date 2017-10-19 09:36:39
	 */
	@RequestMapping("/toUpdateOrderAlipayNotifyLog")
	public String toUpdateOrderAlipayNotifyLog(HttpServletRequest request) {
		// 编辑操作
		request.setAttribute("opFlag", "edit");
		long id = Long.valueOf(request.getParameter("id"));
		try {
			OrderAlipayNotifyLogEntity orderAlipayNotifyLog = orderAlipayNotifyLogService.getOrderAlipayNotifyLogById(id);
			request.setAttribute("orderAlipayNotifyLog", orderAlipayNotifyLog);
		} catch (Exception e) {
			handleException("跳转编辑订单支付宝支付日志页面错误", logger, e);
		}
		return "order/orderAlipayNotifyLog_form";
	}


	/**
	 * 新增或保存订单支付宝支付日志
	 * @param request 请求对象
	 * @param orderAlipayNotifyLogEntity 订单支付宝支付日志实体
	 * @return
	 * @author llp
 	 * @date 2017-10-19 09:36:39
	 */
	@RequestMapping("/saveOrUpdateOrderAlipayNotifyLog")
	@ResponseBody
	public Map<String, Object> saveOrderAlipayNotifyLog(HttpServletRequest request, OrderAlipayNotifyLogEntity orderAlipayNotifyLogEntity) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		int result = 0;
		try {
			String id = request.getParameter("id");
			if(StringUtil.isNotEmpty(id)){
				result = orderAlipayNotifyLogService.updateOrderAlipayNotifyLog(orderAlipayNotifyLogEntity);
			}else{
				result = (int)orderAlipayNotifyLogService.saveOrderAlipayNotifyLog(orderAlipayNotifyLogEntity);
			}
			resultMap.put("code", result);
		} catch (Exception e) {
			handleException("添加订单支付宝支付日志失败", logger, e);
		}
		return resultMap;
	}
	
}
